<?php
class ClassService extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}
	
	//获取分类列表
	function getClassList($pid,$page){
		$page=$page?($page-1):0;
		$offset=15;
		
		$limit=$page*$offset.','.$offset;
		//echo $limit;
		$this->db->where('pid',$pid);
		$this->db->from('class');
		
		$this->db->limit($offset,$page*$offset);
		$query = $this->db->get();
		//echo $this->db->last_query();
		$list= $query->result();
		
		//获取子分类
		foreach($list as $key=>&$val){
			$this->db->where('pid',$val->id);
			$this->db->from('class');
			$query = $this->db->get();
			$val->list= $query->result();
		}
		
		//获取分页字符串
		$this->db->where('pid',$pid);
		$this->db->from('class');
		$total = $this->db->count_all_results();
		
		$this->load->library('pagination');
		$pageHtml=$this->pagination->create_links($total);
		
		return array($list,$pageHtml);
	}
	
	//获取所有分类列表
	function getAllClassList(){
		$this->db->from('class');
		$query = $this->db->get();
		$list= $query->result();
	
		//获取子分类
		foreach($list as $key=>&$val){
			$this->db->where('pid',$val->id);
			$this->db->from('class');
			$query = $this->db->get();
			$val->list= $query->result();
		}
	
		return $list;
	}
	//获取一级分类列表
	function getFirstClassList(){
		$this->db->where('pid',0);
		$this->db->from('class');
		$query = $this->db->get();
	
		return $query->result();
	}
	//获取分页字符串
	
	//保存分类
	function save($data){
		$this->db->insert('class', $data);
		
	}
	
}